package p270jp.pxv.android.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import p104de.greenrobot.dao.query.QueryBuilder;
import p104de.greenrobot.dao.query.WhereCondition;
import p270jp.pxv.android.legacy.constant.ContentType;
import p270jp.pxv.android.legacy.model.DaoManager;
import p270jp.pxv.android.legacy.model.SearchHistory;
import p270jp.pxv.android.legacy.model.SearchHistoryDao;

/* renamed from: jp.pxv.android.model.SearchHistoryDaoManager */
/* loaded from: classes2.dex */
public class SearchHistoryDaoManager {
    private final DaoManager daoManager;

    public SearchHistoryDaoManager(DaoManager daoManager) {
        this.daoManager = daoManager;
    }

    private List<String> getSearchHistories() {
        QueryBuilder<SearchHistory> queryBuilder = this.daoManager.getReadableSession().getSearchHistoryDao().queryBuilder();
        queryBuilder.m7240h(" DESC", SearchHistoryDao.Properties.CreatedAt);
        List<SearchHistory> f = queryBuilder.m7242f();
        ArrayList arrayList = new ArrayList();
        for (SearchHistory searchHistory : f) {
            arrayList.add(searchHistory.getQuery());
        }
        return arrayList;
    }

    public void deleteAllSearchHistory() {
        this.daoManager.getWritableSession().getSearchHistoryDao().deleteAll();
    }

    public void deleteSearchHistory(String str) {
        SearchHistoryDao searchHistoryDao = this.daoManager.getWritableSession().getSearchHistoryDao();
        QueryBuilder<SearchHistory> queryBuilder = searchHistoryDao.queryBuilder();
        queryBuilder.m7238j(SearchHistoryDao.Properties.Query.m7284a(str), new WhereCondition[0]);
        for (SearchHistory searchHistory : queryBuilder.m7242f()) {
            searchHistoryDao.delete(searchHistory);
        }
    }

    public List<String> getUniqueSearchHistories() {
        ArrayList arrayList = new ArrayList();
        for (String str : getSearchHistories()) {
            if (!arrayList.contains(str)) {
                arrayList.add(str);
            }
        }
        return arrayList;
    }

    public void updateSearchHistory(String str, ContentType contentType) {
        SearchHistoryDao searchHistoryDao = this.daoManager.getWritableSession().getSearchHistoryDao();
        QueryBuilder<SearchHistory> queryBuilder = searchHistoryDao.queryBuilder();
        queryBuilder.m7238j(SearchHistoryDao.Properties.Query.m7284a(str), SearchHistoryDao.Properties.ContentType.m7284a(contentType.toString()));
        List<SearchHistory> f = queryBuilder.m7242f();
        if (!f.isEmpty()) {
            SearchHistory searchHistory = f.get(0);
            searchHistory.setCreatedAt(new Date());
            searchHistoryDao.update(searchHistory);
            return;
        }
        searchHistoryDao.insert(new SearchHistory(null, str, contentType.toString(), new Date()));
    }

    private void deleteSearchHistory(String str, ContentType contentType) {
        SearchHistoryDao searchHistoryDao = this.daoManager.getWritableSession().getSearchHistoryDao();
        QueryBuilder<SearchHistory> queryBuilder = searchHistoryDao.queryBuilder();
        queryBuilder.m7238j(SearchHistoryDao.Properties.Query.m7284a(str), SearchHistoryDao.Properties.ContentType.m7284a(contentType.toString()));
        for (SearchHistory searchHistory : queryBuilder.m7242f()) {
            searchHistoryDao.delete(searchHistory);
        }
    }
}
